<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/7/23
 * Time: 14:00
 */
namespace App\Http\Controllers\Admin\Log;

use \App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminiLogController extends Controller
{
    /**
     * 登录首页
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @param   <null>
     * @return  <view>  [后台登录页面]
     */
    public function index()
    {
        //输出页面
        return view('Admin.Log.log');
    }

    /**
     * 验证码
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @param   <null>
     * @return  <img>  [验证码]
     */
    public function captcha()
    {
        //实例化验证码类
        $captcha = new \CaptchaClass();
        //生成验证码
        $captcha->doimg();
        //存入session
        session(['Z_AdminiUser_Captcha' => $captcha->getCode()]);
    }

    /**
     * 验证登录
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @param   <Request>                           [$request 用户登录提交数据]
     * @return  <redirect success page or error>    [成功登录后台首页或者报错]
     */
    public function check(Request $request)
    {
        //验证post提交
        if($request->isMethod('post'))
        {
            //获取提交数据
            $data['login'] = $request->all();
            //验证验证码
            if(empty($data['login']['captcha']) || session('Z_AdminiUser_Captcha') != strtolower($data['login']['captcha']))
            {
                $data['error'] = '<i class=\'fa fa-times-circle-o\'></i>验证码错误!&nbsp;';
                return view('Admin.Log.log')->withData($data);
            //账号密码为空验证
            }else if(empty($data['login']['username']) || empty($data['login']['password'])){
                $data['error'] = '<i class=\'fa fa-times-circle-o\'></i>账户名或密码不能为空!&nbsp;';
                return view('Admin.Log.log')->withData($data);
            }else{
                $res = DB::select('
                        select id,username,password,group_master_id,group_servant_id from wj_adminuser
                        where status = 1
                        and username = ?
                    ',[$data['login']['username']]);
                //数据库账号验证
                if(empty($res[0]->id) || empty($res[0]->username) || empty($res[0]->password))
                {
                    $data['error'] = '<i class=\'fa fa-times-circle-o\'></i>账户名或密码错误!&nbsp;';
                    return view('Admin.Log.log')->withData($data);
                }else if(!password_verify($data['login']['password'],$res[0]->password))
                {
                    $data['error'] = '<i class=\'fa fa-times-circle-o\'></i>账户名或密码错误!&nbsp;';
                    return view('Admin.Log.log')->withData($data);
                }else{
                    unset($res[0]->password);
                    session(['Z_AdminiUser' => json_encode($res[0],true)]);
                    \Helpers::insertUserActionLog('adminLogin','login','','success',$res,1);
                    header('Location:'.__BASE__.'/admini');
                }
            }
        }else{
            abort(404);
        }
    }

    /**
     * 后台登出...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <2016-10-29 10:10>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:后台登出>
     * @param Request $request
     */
    public function logout(Request $request)
    {
        \Helpers::insertUserActionLog('adminLogin','logout','','success');
        $request->session()->forget('Z_AdminiUser');
        if(empty(session('Z_AdminiUser')))
        {
            echo 1;
        }else{
            echo -1;
        }
    }

}